package com.zwz.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwz.bean.TourismInfo;
import com.zwz.controller.util.Result;
import com.zwz.service.TourismInfoService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Alysia
 * @createDate 2022/11/14 11:14
 * @description 旅游信息相关控制器
 */

@RestController
@RequestMapping("/tourism")
public class TourismController {

    @Resource
    private TourismInfoService tourismInfoService;


    /**
     * 增加一条旅游信息
     *
     * @param tourismInfo 旅游信息
     * @return 成功标志
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody TourismInfo tourismInfo) {
        if (tourismInfoService.save(tourismInfo)) {
            return Result.success("发布成功！");
        } else {
            return Result.error("服务器错误!");
        }
    }

    /**
     * 删除一条旅游信息
     *
     * @param tourismId 旅游id
     * @return 成功标志
     */
    @DeleteMapping("/delete/{tourismId}")
    public Result delete(@PathVariable Integer tourismId) {
        if (tourismInfoService.removeById(tourismId)) {
            return Result.success("删除成功！");
        } else {
            return Result.error("服务器错误！");
        }
    }

    /**
     * 修改旅游信息
     *
     * @param tourismInfo 修改后的旅游信息
     * @return 成功标志
     */
    @PutMapping("/update")
    public Result update(@RequestBody TourismInfo tourismInfo) {
        if (tourismInfoService.updateById(tourismInfo)) {
            return Result.success("修改成功！");
        } else {
            return Result.error("服务器错误！");
        }
    }

    /**
     * 展示某一条数据
     *
     * @param tourismId 旅游信息id
     * @return 一条旅游信息数据
     */
    @GetMapping("/show/{tourismId}")
    public Result show(@PathVariable Integer tourismId) {
        TourismInfo tourismInfo = tourismInfoService.getById(tourismId);
        return Result.success(tourismInfo);
    }

    /**
     * 分页查询相关
     *
     * @param currentPage 当前页码
     * @param pageSize    每页条数
     * @param tourismInfo 查询条件
     * @return 一页旅游信息数据
     */
    @GetMapping("/page/{currentPage}/{pageSize}")
    public Result page(@PathVariable int currentPage, @PathVariable int pageSize, TourismInfo tourismInfo) {
        IPage<TourismInfo> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<TourismInfo> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(tourismInfo.getTourismTitle()), TourismInfo::getTourismTitle, tourismInfo.getTourismTitle());
        lqw.like(Strings.isNotEmpty(tourismInfo.getTourismContents()), TourismInfo::getTourismContents, tourismInfo.getTourismContents());

        return Result.success(tourismInfoService.page(page, lqw));
    }
}
